import Vue from 'vue'
import Vuex from 'vuex'

//导入依赖
import VuexPersistence from 'vuex-persist'


Vue.use(Vuex)
//实例化对象
const vuexLocal = new VuexPersistence({
    storage: window.localStorage
})


export default new Vuex.Store({
  state: {
     todolist:[],
     mnCart:[],
     user_list:[],
  },
  mutations: {
    //todolist
    addtodo(state,todo){
      console.log(todo);
      state.todolist.unshift(todo)
    },
    //修改状态
    changeStatus(state,check){
      console.log(check);
      //将这个对象的值分别赋值给这两个变量
      let {status,index} = check;
      console.log(status,index);
      //按索引值，修改已完成的状态
      state.todolist[index].status = status;
    },
    //全选
    checkAll(state,checked){
      console.log(checked);
        state.todolist.map(item=>{
          item.checked = checked;
        })
    },
    // 模拟加入购物车
    addCart1(state,goods){
      state.mnCart.unshift(goods);
    },
    //购物车的删除
    del(state,index){
      console.log(index);
      state.mnCart.splice(index,1)
    },
    addUser(state,data){
      state.user_list.push(data)
    },
    delUser(state,index){
      state.user_list.splice(index)
    }

  },
  actions: {

  },
  getters:{
    //计算未完成的总条数 
      numOne(state){
        let num1 = 0;
        state.todolist.forEach(item=>{
          if (item.status == 1) {
            num1++
          }
        })
        return num1;
      },
      //计算选中状态的总条数 
      numTow(state){
        let num2 = 0;
        state.todolist.forEach(item=>{
          if (item.checked == true) {
              num2++
          }
        })
        return num2;
      }
  },
  plugins: [vuexLocal.plugin],


})
